package com.yh.hljapp.modules.adatper;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.hljapp.R;
import com.yh.hljapp.common.StringUtils;
import com.yh.hljapp.component.dialog.DialogHelp;
import com.yh.hljapp.gddb.gdbean.Add_bodyBack_Bean;
import com.yh.hljapp.modules.recycleviewutil.onMoveAndSwipedListener;
import com.yh.hljapp.modules.recycleviewutil.onStateChangedListener;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：ADDBackMatAdapter
 * <p>
 * 创建时间：2017.06.03.10:03
 * *****************************
 */
public class Add_Saleback_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements onMoveAndSwipedListener {

    private Context mContext;
    private ArrayList<Add_bodyBack_Bean> workInfos;
    private OnSaleAddAdapterListern listener;

    public Add_Saleback_Adapter(Context context, ArrayList<Add_bodyBack_Bean> checkedCKS, OnSaleAddAdapterListern listener) {
        this.mContext = context;
        this.workInfos = checkedCKS;
        this.listener = listener;
    }


    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        return false;
    }

    @Override
    public void onItemDismiss(int position) {
        //删除mItems数据
        workInfos.remove(position);
        //删除RecyclerView列表对应item
        notifyItemRemoved(position);
    }


    public interface OnSaleAddAdapterListern {
        void onFHCKClick(Add_bodyBack_Bean bean, int position);

    }

    public ArrayList<Add_bodyBack_Bean> getBackList() {
        return workInfos;
    }

    public void setBackList(ArrayList<Add_bodyBack_Bean> workInfos) {
        this.workInfos = workInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sale_out, parent,
                false);
        return new BackViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BackViewHolder) {
            Add_bodyBack_Bean backMatBean = workInfos.get(position);
            initDataView((BackViewHolder) holder, backMatBean, position);
        }
    }

    private void initDataView(final BackViewHolder holder, final Add_bodyBack_Bean bean, final int position) {

        if (!StringUtils.isEmpty(bean.getTDCStockID())) {
            holder.tvFhWare.setText(bean.getTDCStockName());//发货仓库
        } else {
            holder.tvFhWare.setText(bean.getFDCStockName());//发货仓库
        }

        holder.tvCcCode.setText(bean.getNumber());
        holder.tvSfNum.setText(bean.getCount());
        holder.tvGgModle.setText(bean.getModel());
        holder.tvCcName.setText(bean.getName());
        holder.tvUnit.setText(bean.getUnit_name());

        holder.llFhck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onFHCKClick(bean, position);
                }
            }
        });

        ((BackViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                DialogHelp.getConfirmDialog(mContext, "确定删除  " + bean.getName() + "  么？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        onItemDismiss(position);
                    }
                }).show();
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return workInfos.size();
    }


    class BackViewHolder extends RecyclerView.ViewHolder implements onStateChangedListener {
        @InjectView(R.id.tv_cc_code)
        TextView tvCcCode;
        @InjectView(R.id.tv_cc_name)
        TextView tvCcName;
        @InjectView(R.id.tv_gg_modle)
        TextView tvGgModle;
        @InjectView(R.id.tv_unit)
        TextView tvUnit;
        @InjectView(R.id.tv_sf_num)
        TextView tvSfNum;

        @InjectView(R.id.tv_fh_ware)
        TextView tvFhWare;
        @InjectView(R.id.ll_fhck)
        LinearLayout llFhck;


        public BackViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

        @Override
        public void onItemSelected() {
            //设置item的背景颜色为浅灰色
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemClear() {
            //恢复item的背景颜色
            itemView.setBackgroundColor(0);
        }
    }
}
